
using Qdb.Config;
using Qdb.Ext;
using Qdb.Foundation;
using Qdb.Drs.Qdb;
using Qdb.Drs.Inside;
using Qdb.Drs.Tests;
using Sharpen.IO;

namespace Qdb.Drs.Tests
{
	public class QdbDrsFixture : IDrsFixture
	{
		internal static readonly File RamDrive = new File("w:");

		protected string _name;

		protected IExtObjectContainer _db;

		protected ITestableReplicationProviderInside _provider;

		protected readonly File testFile;

		private IConfiguration _config;

		public QdbDrsFixture(string name)
		{
			_name = name;
			if (RamDrive.Exists())
			{
				testFile = new File(RamDrive.GetPath() + "drs_cs_" + _name + ".qdb");
			}
			else
			{
				testFile = new File("drs_cs_" + _name + ".qdb");
			}
		}

		public virtual ITestableReplicationProviderInside Provider()
		{
			return _provider;
		}

		public virtual void Clean()
		{
			testFile.Delete();
			_config = null;
		}

		public virtual void Close()
		{
			_provider.Destroy();
			_db.Close();
		}

		public virtual IExtObjectContainer Db()
		{
			return _db;
		}

		public virtual void Open()
		{
			_db = QdbFactory.OpenFile(CloneConfiguration(), testFile.GetPath
				()).Ext();
			_provider = QdbProviderFactory.NewInstance(_db, _name);
		}

		private IConfiguration CloneConfiguration()
		{
			return (IConfiguration)((IDeepClone)Config()).DeepClone(null);
		}

		public virtual IConfiguration Config()
		{
			if (_config == null)
			{
				_config = QdbFactory.NewConfiguration();
			}
			return _config;
		}
	}
}
